package com.gds.wiki.cloud.admin.server.security.oauth2.config;


import com.gds.wiki.cloud.admin.server.security.oauth2.core.service.LoginAuthenticationProvider;
import com.gds.wiki.cloud.framework.web.constant.HttpSecurityConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

/**
 * @author WIKI
 * Date 2018-04-19
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Autowired
    private LoginAuthenticationProvider provider;


    @Bean
    protected AuthenticationManager authenticationManager() {
        return new AuthenticationManager() {
            @Override
            public Authentication authenticate(Authentication authentication) throws AuthenticationException {
                return provider.authenticate(authentication);
            }
        };
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.cors().and()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers(HttpSecurityConstant.swagger_ui_matchers()).permitAll()
                .antMatchers(HttpSecurityConstant.user_white_list()).permitAll()
                .anyRequest().authenticated()
                .and()
        ;
//                .antMatchers("/oauth2/token").permitAll()

    }

}
